//
//  LBWEmoticonLeyboard.swift
//  weibo
//
//  Created by Limonwei on 15/12/15.
//  Copyright © 2015年 heima. All rights reserved.
//

//整个表情键盘的视图 可分为底部工具栏 放置表情的collectionView 页码控制器

import UIKit

//可重用ID
private let LBWEmoticonCollectionViewId = "LBWEmoticonCollectionViewId"

class LBWEmoticonLeyboard: UIView {

    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //初始化视图
    private func setupUI(){
        //背景颜色
        backgroundColor = UIColor(patternImage: UIImage(named: "emoticon_keyboard_background")!)
        
        //添加子控件
        addSubview(toolBar)
        addSubview(emoticonCollectionView)
        addSubview(emoticonPageControl)
        
        //添加约束
        toolBar.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(self)
            make.trailing.equalTo(self)
            make.bottom.equalTo(self)
            make.height.equalTo(37)
        }
        
        emoticonCollectionView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(self)
            make.width.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
            make.leading.equalTo(self)
        }
        
        emoticonPageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self)
            make.bottom.equalTo(toolBar.snp_top)
        }
        
        //让界面完全显示之后,去滚动 
        //主线程异步里面的代码会在其他主线程逻辑完成之后执行
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            
            //将collectionView滚动到指定组
            self.emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
            
            self.setPageControlData(indexPath)
        }

        
        
    }
    
    //在layoutSubview里计算item的大小
    override func layoutSubviews() {
        super.layoutSubviews()
        //取到layout 设置itemSize
        let layout = emoticonCollectionView.collectionViewLayout as! UICollectionViewFlowLayout
        layout.itemSize = emoticonCollectionView.size
    }
    
    
    //懒加载控件
    //1.底部工具条
    private lazy var toolBar: LBWEmoticonToolBar = {
    
        let toolBar = LBWEmoticonToolBar(frame: CGRectZero)
        //成为代理,监听点击
        toolBar.delegate = self
        return toolBar
    
    }()
    
    //2.显示表情视图
    private lazy var emoticonCollectionView: UICollectionView = {
        
        //初始化layout
        let layout = UICollectionViewFlowLayout()
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        
        //设置滚动方向
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        let collectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: layout)
        //设置颜色
        collectionView.backgroundColor = UIColor.clearColor()
        
        //注册cell
        collectionView.registerClass(LBWEmoticonpageView.self, forCellWithReuseIdentifier: LBWEmoticonCollectionViewId)
        //设置数据源
        collectionView.dataSource = self
        collectionView.delegate = self
        
        //隐藏滚动条
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        
        //开启分页
        collectionView.pagingEnabled = true
        //水平方向的弹簧效果关闭
        collectionView.bounces = false
        
        return collectionView
        
    }()
    
    //3.pageControl
    private lazy var emoticonPageControl: UIPageControl = {
       
        let pageControl = UIPageControl()
        
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKeyPath: "_currentPageImage")
        pageControl.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKeyPath: "_pageImage")
        
        return pageControl
    }()
}


//LBWEmoticonToolBarDelegate 按钮点击的方法
extension LBWEmoticonLeyboard: LBWEmoticonToolBarDelegate{
    
    func didSelectedButtonWithType(type: LBWEmoticonType) {
    //常量有一次赋值的机会
        let indexPath: NSIndexPath
        
        switch type{
        case .Recrnt:
            indexPath = NSIndexPath(forItem: 0, inSection: 0)
        case .Default:
            indexPath = NSIndexPath(forItem: 0, inSection: 1)
        case .Emoji:
            indexPath = NSIndexPath(forItem: 0, inSection: 2)
        case .Lxh:
            indexPath = NSIndexPath(forItem: 0, inSection: 3)
        }
        //将collectionView滚动到指定组
        emoticonCollectionView.scrollToItemAtIndexPath(indexPath, atScrollPosition: .Left, animated: false)
        
        
        setPageControlData(indexPath)
    }
}



//表情键盘的数据源代理方法
extension LBWEmoticonLeyboard: UICollectionViewDelegate, UICollectionViewDataSource {
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return LBWEmoticonTools.shareTools.allEmoticons.count
    }
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        //返回表情多少页
        return LBWEmoticonTools.shareTools.allEmoticons[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(LBWEmoticonCollectionViewId, forIndexPath: indexPath) as! LBWEmoticonpageView
        
        cell.indexPath = indexPath
        
        cell.emoticons = LBWEmoticonTools.shareTools.allEmoticons[indexPath.section] [indexPath.item]
        
        return cell
    }
    
    //监听滚动
    func scrollViewDidScroll(scrollView: UIScrollView) {
        //取到界面上显示的cell
        let cells = emoticonCollectionView.visibleCells()
        //判断哪一个cell在界面上显示的多
        if cells.count == 2 {
            //取到两个cell
            let firstCell = cells.first!
            let secondCell = cells.last!
            
            // 判断哪一个 cell 在界面上显示得多 --> 怎么判断
            // contenOffset
            // 拿 cell 的 x 与 contentOffset.x 值进入相差,取绝对值
            // 哪个绝对值小,哪一个就显示得多
            
            let firstCellR = abs(Int32(firstCell.x - scrollView.contentOffset.x))
            
            let secondCellR = abs(Int32(secondCell.x - scrollView.contentOffset.x))
            
            let cell: UICollectionViewCell
            if firstCellR < secondCellR {
                //第一个显示的多
                cell = firstCell
            }else{
                //第二个显示的多
                cell = secondCell
            }
            // 根据哪一个cell 在界面显得多,取到其在 collectionView 中的 indexPath

            let indexPath = emoticonCollectionView.indexPathForCell(cell)!
            
            toolBar.currentSection = indexPath.section
            
            setPageControlData(indexPath)
        }
    }
    
    private func setPageControlData(indexPath: NSIndexPath){
        //设置pageControl
        emoticonPageControl.numberOfPages = LBWEmoticonTools.shareTools.allEmoticons[indexPath.section].count
        
        emoticonPageControl.currentPage = indexPath.item
        
        //如果是第0组,就隐藏
        emoticonPageControl.hidden = indexPath.section == 0
    }
}








































